/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.PaymentRate;
import ses.exceptions.IllegalOrphanException;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.PaymentDetails;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class PaymentRateJpaController implements Serializable {

    public PaymentRateJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PaymentRate paymentRate) throws RollbackFailureException, Exception {
        if (paymentRate.getPaymentDetailsList() == null) {
            paymentRate.setPaymentDetailsList(new ArrayList<PaymentDetails>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            List<PaymentDetails> attachedPaymentDetailsList = new ArrayList<PaymentDetails>();
            for (PaymentDetails paymentDetailsListPaymentDetailsToAttach : paymentRate.getPaymentDetailsList()) {
                paymentDetailsListPaymentDetailsToAttach = em.getReference(paymentDetailsListPaymentDetailsToAttach.getClass(), paymentDetailsListPaymentDetailsToAttach.getPaymentDetailsPK());
                attachedPaymentDetailsList.add(paymentDetailsListPaymentDetailsToAttach);
            }
            paymentRate.setPaymentDetailsList(attachedPaymentDetailsList);
            em.persist(paymentRate);
            for (PaymentDetails paymentDetailsListPaymentDetails : paymentRate.getPaymentDetailsList()) {
                PaymentRate oldPaymentRateOfPaymentDetailsListPaymentDetails = paymentDetailsListPaymentDetails.getPaymentRate();
                paymentDetailsListPaymentDetails.setPaymentRate(paymentRate);
                paymentDetailsListPaymentDetails = em.merge(paymentDetailsListPaymentDetails);
                if (oldPaymentRateOfPaymentDetailsListPaymentDetails != null) {
                    oldPaymentRateOfPaymentDetailsListPaymentDetails.getPaymentDetailsList().remove(paymentDetailsListPaymentDetails);
                    oldPaymentRateOfPaymentDetailsListPaymentDetails = em.merge(oldPaymentRateOfPaymentDetailsListPaymentDetails);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PaymentRate paymentRate) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            PaymentRate persistentPaymentRate = em.find(PaymentRate.class, paymentRate.getPaymentId());
            List<PaymentDetails> paymentDetailsListOld = persistentPaymentRate.getPaymentDetailsList();
            List<PaymentDetails> paymentDetailsListNew = paymentRate.getPaymentDetailsList();
            List<String> illegalOrphanMessages = null;
            for (PaymentDetails paymentDetailsListOldPaymentDetails : paymentDetailsListOld) {
                if (!paymentDetailsListNew.contains(paymentDetailsListOldPaymentDetails)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PaymentDetails " + paymentDetailsListOldPaymentDetails + " since its paymentRate field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<PaymentDetails> attachedPaymentDetailsListNew = new ArrayList<PaymentDetails>();
            for (PaymentDetails paymentDetailsListNewPaymentDetailsToAttach : paymentDetailsListNew) {
                paymentDetailsListNewPaymentDetailsToAttach = em.getReference(paymentDetailsListNewPaymentDetailsToAttach.getClass(), paymentDetailsListNewPaymentDetailsToAttach.getPaymentDetailsPK());
                attachedPaymentDetailsListNew.add(paymentDetailsListNewPaymentDetailsToAttach);
            }
            paymentDetailsListNew = attachedPaymentDetailsListNew;
            paymentRate.setPaymentDetailsList(paymentDetailsListNew);
            paymentRate = em.merge(paymentRate);
            for (PaymentDetails paymentDetailsListNewPaymentDetails : paymentDetailsListNew) {
                if (!paymentDetailsListOld.contains(paymentDetailsListNewPaymentDetails)) {
                    PaymentRate oldPaymentRateOfPaymentDetailsListNewPaymentDetails = paymentDetailsListNewPaymentDetails.getPaymentRate();
                    paymentDetailsListNewPaymentDetails.setPaymentRate(paymentRate);
                    paymentDetailsListNewPaymentDetails = em.merge(paymentDetailsListNewPaymentDetails);
                    if (oldPaymentRateOfPaymentDetailsListNewPaymentDetails != null && !oldPaymentRateOfPaymentDetailsListNewPaymentDetails.equals(paymentRate)) {
                        oldPaymentRateOfPaymentDetailsListNewPaymentDetails.getPaymentDetailsList().remove(paymentDetailsListNewPaymentDetails);
                        oldPaymentRateOfPaymentDetailsListNewPaymentDetails = em.merge(oldPaymentRateOfPaymentDetailsListNewPaymentDetails);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = paymentRate.getPaymentId();
                if (findPaymentRate(id) == null) {
                    throw new NonexistentEntityException("The paymentRate with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            PaymentRate paymentRate;
            try {
                paymentRate = em.getReference(PaymentRate.class, id);
                paymentRate.getPaymentId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The paymentRate with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<PaymentDetails> paymentDetailsListOrphanCheck = paymentRate.getPaymentDetailsList();
            for (PaymentDetails paymentDetailsListOrphanCheckPaymentDetails : paymentDetailsListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This PaymentRate (" + paymentRate + ") cannot be destroyed since the PaymentDetails " + paymentDetailsListOrphanCheckPaymentDetails + " in its paymentDetailsList field has a non-nullable paymentRate field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(paymentRate);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PaymentRate> findPaymentRateEntities() {
        return findPaymentRateEntities(true, -1, -1);
    }

    public List<PaymentRate> findPaymentRateEntities(int maxResults, int firstResult) {
        return findPaymentRateEntities(false, maxResults, firstResult);
    }

    private List<PaymentRate> findPaymentRateEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from PaymentRate as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PaymentRate findPaymentRate(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PaymentRate.class, id);
        } finally {
            em.close();
        }
    }

    public int getPaymentRateCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from PaymentRate as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
